using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QxFramework.Core;

public class EnemyManager : LogicModuleBase, IEnemyManager
{
    List<EnemyControl> enemies = new List<EnemyControl>();
    string Path = "Prefabs/Enemy/";

    public void InstantiateEnemy(string path, Vector2 pos)
    {
        GameObject obj = ResourceManager.Instance.Instantiate(Path + path);
        obj.transform.position = pos;
        RegisterEnemy(obj.GetComponent<EnemyControl>());
    }

    public void RegisterEnemy(EnemyControl enemy)
    {
        enemies.Add(enemy);
    }

    public float GetHealth(EnemyControl enemy)
    {
        return enemy.health;
    }
    public float GetMaxHealth(EnemyControl enemy)
    {
        return enemy.maxHealth;
    }

    public void HealthChange(EnemyControl enemy, float value)
    {
        if (enemy.health + value > enemy.maxHealth)
        {
            enemy.health = enemy.maxHealth;
        }
        else if (enemy.health + value <= 0)
        {
            enemy.health = 0;
            Die(enemy);
        }
        else
        {
            enemy.health += value;
        }
    }

    public void MaxHealthChange(EnemyControl enemy, float value)
    {
        if (enemy.maxHealth + value < 0)
        {
            Debug.LogError("Player's max health can't go below zero!");
        }
        else
        {
            enemy.maxHealth += value;
        }
    }

    public void Damage(EnemyControl enemy, float damage, DamageArgs args = null)
    {
        enemy.isHatred = true;
        HealthChange(enemy, -damage);
        if(args!=null)
        {
            GameMgr.Get<IEffectManager>().InstantiateEffect("BloodEffect", args.source, args.directionAngle);
        }
    }

    public void Die(EnemyControl enemy)
    {
        GameMgr.Get<IPlayerManager>().Player.Money += (int)(enemy.money * enemy.size);
        GameMgr.Get<IEffectManager>().InstantiateEffect("BloodEffect", enemy.transform.position);
        GameMgr.Get<IEffectManager>().InstantiateEffect("BloodEffect", enemy.transform.position);
        enemy.isDead = true;
        enemies.Remove(enemy);
        GameObject.Destroy(enemy.gameObject);
    }

    public List<EnemyControl> Enemies
    {
        get
        {
            return enemies;
        }
    }
}
